using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections.Specialized;
using System.Collections;
using System.Web;

namespace CodePlex.Resourceful.Bookmarks.Model
{
    public class UserAccount
    {
        private string _name;
        private string _fullName;
        private string _email;
        private string _password;


        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string FullName
        {
            get { return _fullName; }
            set { _fullName = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }



        public override string ToString()
        {
            return string.Format("Name={0},FullName={1},Email={2},Password={3}",
                this.Name,
                this.FullName,
                this.Email,
                this.Password);
        }





       
        public void ToFormUrlEncoded(TextWriter writer)
        {

            NameValuePairs nvc = new NameValuePairs();
            nvc["user[name]"] = this.Name;
            nvc["user[full-name]"] = this.FullName;
            nvc["user[email]"] = this.Email;
            nvc["user[password]"] = this.Password;



            string rt = Fn.Join("&",
                 Fn.Select<string, string>(nvc.Keys, delegate(string item)
                {
                    return string.Format("{0}={1}", item,  HttpUtility.UrlEncode(nvc[item],Encoding.UTF8));
                })
             );


           writer.WriteLine(rt);
           writer.Flush();
        }


        public void ToXml( XmlTextWriter xwriter)
        {
            
           using (Utils.StartElement( xwriter,"user"))
           {
                xwriter.WriteElementString("name", this.Name);
                xwriter.WriteElementString("full-name", this.FullName);
                xwriter.WriteElementString("email", this.Email);
                xwriter.WriteElementString("password", this.Password);

           }

            xwriter.Flush();

        }


        public static UserAccount FromXml(TextReader reader)
        {

            UserAccount rt = new UserAccount();

            XmlTextReader xreader = new XmlTextReader(reader);

            while (xreader.Read())
            {
                if (xreader.Name == "name")
                {
                    rt.Name = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "full-name")
                {
                    rt.FullName = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "email")
                {
                    rt.Email = xreader.ReadElementContentAsString();
                }
                if (xreader.Name == "password")
                {
                    rt.Password = xreader.ReadElementContentAsString();
                }

            }

            return rt;

        }

       

        public string ToFormUrlEncodedString()
        {
            StringWriter writer = new StringWriter();
            this.ToFormUrlEncoded(writer);
            return writer.ToString();
        }
    }
}
